package com.example.dell.fastshopping.utils;

import com.example.dell.fastshopping.jiguang.PushMessage;
import com.example.dell.fastshopping.user.entity.UserInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${YSH} on 2019/5/15.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         key 未抽取
 */
public class SPDao {

    //保存竞拍场 我交过的保证金，首页会进行权限校验
    public static void saveAmounts(List<UserInfo.Amounts> amountsList) {

        String s = new Gson().toJson(amountsList);

        KLog.e(s);

        SPUtils.getInstance().put("amountsList", s);

    }

    //设置是否要登陆
    public static void setShouldLogin(boolean b) {
        SPUtils.getInstance().put("isFirst", b);
    }

    public static boolean getShouldLogin() {
        return SPUtils.getInstance().getBoolean("isFirst");
    }

    public static List<UserInfo.Amounts> getAmountsList() {
        String amountsList = SPUtils.getInstance().getString("amountsList");
        KLog.e(amountsList);
        List<UserInfo.Amounts> list = new Gson().fromJson(amountsList, new TypeToken<List<UserInfo.Amounts>>() {
        }.getType());
        return list;
    }

    public static String getUserPhone() {
        return SPUtils.getInstance().getString("phone");
    }

    public static void saveUserPhone(String phone) {
        SPUtils.getInstance().put("phone", phone);
    }

    public static void saveUserId(int id) {
        SPUtils.getInstance().put("userId", id);
    }

    public static int getUserId() {
        return SPUtils.getInstance().getInt("userId", -1);
    }


    public static void savePushMessage(PushMessage message) {
        List<PushMessage> pushMessage = getPushMessage();
        if (pushMessage == null) {
            pushMessage = new ArrayList<>();
            pushMessage.add(message);
        } else {
            if (pushMessage.size() > 19) {
                pushMessage.remove(pushMessage.size() - 1);
            }
            pushMessage.add(0, message);
        }
        String json = new GsonBuilder().create().toJson(pushMessage);
        SPUtils.getInstance().put("push_message", json);
    }


    public static List<PushMessage> getPushMessage() {
        String message = SPUtils.getInstance().getString("push_message", "");
        if (message.isEmpty()) {
            return null;
        } else {
            return new Gson().fromJson(message, new TypeToken<List<PushMessage>>() {
            }.getType());
        }
    }

    public static void saveUnReadMessageNum() {
        int num = getUnReadMessageNum();
        num++;
        SPUtils.getInstance().put("unread_message_num", num);
    }

    public static int getUnReadMessageNum() {
        int num = SPUtils.getInstance().getInt("unread_message_num", 0);
        return num;
    }

    public static void clearMessageNum() {
        SPUtils.getInstance().put("unread_message_num", 0);
    }
}
